﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Web.Mvc;

namespace Helpers
{
    public class SmsHelpers
    {
        private readonly string _username;
        private readonly string _password;
        private readonly string _brandname;

        public SmsHelpers()
        {
            _username = ConfigurationManager.AppSettings["sms_username"];
            _password = ConfigurationManager.AppSettings["sms_password"];
            _brandname = ConfigurationManager.AppSettings["sms_brandname"];
        }
        
        
        public SmsResultObject Send(string messid, string phonenumber, string mes,out string smsresult)
        {
            try
            {
                const string url = "http://api.bipbip.vn/api/send";

                var obj = new
                {
                    username = _username,
                    password = _password,
                    message = mes,
                    brandname = _brandname,
                    recipients = new List<object>
                                               {
                                                    new {
                                                   message_id = messid.ToString(),
                                                   number = phonenumber
                                                   }
                                               }

                };
                var data = JsonHelpers.Serialize(obj);
                var retval = HtmlHelpers.PostJson(url, data);

                var result = JsonHelpers.Deserialize<List<SmsResultObject>>(retval);
                smsresult = retval;
                if (result.Count > 0)
                {
                    return result[0];
                }
            }
            catch (Exception ex)
            {
                smsresult = ex.Message;
            }
            
            return null;
        }
        /*
        public SmsResultObject SendSedule(string messid, string phonenumber, string mes, DateTime time)
        {

            const string url = "http://api.bipbip.vn/api/schedule";

            var obj = new
            {
                username = _username,
                password = _password,
                message = mes,
                brandname = _brandname,
                timesent = string.Format("{0:YYYY/MM/DD hh:mm:ss}", time),
                recipients = new List<object>
                                               {
                                                    new {
                                                   message_id = messid,
                                                   number = phonenumber
                                                   }
                                               }

                //timesent = "2012/05/31 12:05:00"
            };
            var data = JsonHelpers.Serialize(obj);
            var retval = HtmlHelpers.PostJson(url, data);
            var result = JsonHelpers.Deserialize<List<SmsResultObject>>(retval);

            if (result.Count > 0)
            {
                return result[0];
            }

            return null;


        }

        */
        [Serializable]
        public class SmsResultObject
        {
            public string status { get; set; }
            public string message_id { get; set; }
            public string description { get; set; }
        }
    }
}
